package com.agent.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.agent.exception.MyException;
import com.agent.exception.StaticExceptionCodes;
import com.agent.mapper.AgentOrderApplyMapper;
import com.agent.mapper.AgentTravelAgencyMapper;
import com.agent.mapper.TravelAgencySignResourceMapper;
import com.agent.model.AgentOrderApply;
import com.agent.model.TravelAgencySignResource;
import com.agent.service.IAgentOrderApplySer;

@Service("agentOrderApplySer")
public class AgentOrderApplySerImpl implements IAgentOrderApplySer{
	@Autowired
	AgentOrderApplyMapper agentOrderApplyMapper;
	@Autowired
	AgentTravelAgencyMapper agentTravelAgencyMapper;
	@Autowired
	TravelAgencySignResourceMapper travelAgencySignResourceMapper;
	/*提交审核*/
	public int addAgentOrderApply(AgentOrderApply agentOrderApply) throws MyException {
		int result = agentOrderApplyMapper.addAgentOrderApply(agentOrderApply);
		if(result<=0){
			throw new MyException(StaticExceptionCodes.AgentOrderApplyException.applyOrderFail);
		}
		return result;
	}
	/*展示提交审核信息*/
	public List<AgentOrderApply> findListAgentOrderApply(Integer level,Integer agentid) throws MyException {
		List<AgentOrderApply> resultList=null;
		if(level==0){
			resultList = agentOrderApplyMapper.findListAgentOrderApply();
			if(resultList.size()<0){
				throw new MyException(StaticExceptionCodes.AgentOrderApplyException.OrderApplyEmptyError);
			}
			return resultList;
		}
		if(level>0){
			 resultList = agentOrderApplyMapper.findListAgentOrderApplyByAgentid(agentid);
			if(resultList.size()<0){
				throw new MyException(StaticExceptionCodes.AgentOrderApplyException.OrderApplyEmptyError);
			}
			return resultList;
		}
		return resultList;
	}
	
	/*管理员通过审核*/
	public int agentOrderApplyAdopt(AgentOrderApply agentOrderApply) throws MyException {
		int result = agentOrderApplyMapper.updateOrderState(agentOrderApply);
		if(result>0){
			/*更新私海完成状态*/
		agentTravelAgencyMapper.updateAgentTravelAgencyState(agentOrderApply.getTravelAgencyid());
			/*储存信息*/
		TravelAgencySignResource tasr= new TravelAgencySignResource();
		tasr.setAgentid(agentOrderApply.getAgentid());
		tasr.setDescription(agentOrderApply.getDescription());
		tasr.setImagesQualification(agentOrderApply.getImagesQualification());
		travelAgencySignResourceMapper.addTravelAgencySignResource(tasr);
		}else{
			throw new MyException(StaticExceptionCodes.CommonException.systemError);
		}
		return result;
	}
	/*管理员不通过审核*/
	public int agentOrderApplyNotPass(AgentOrderApply agentOrderApply) throws MyException {
		int result = agentOrderApplyMapper.updateOrderStateNotPass(agentOrderApply);
		return result;
	}

}
